package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 144. 二叉树的前序遍历
 给定一个二叉树，返回它的 前序 遍历。
 示例:
 输入: [1,null,2,3]
 1
 \
 2
 /
 3

 输出: [1,2,3]
 */

public class problems_144 {

    public static void main(String[] arge) {
        TreeNode root = new TreeNode(1);
        TreeNode node1 = new TreeNode(3);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(9);
        TreeNode node4 = new TreeNode(3);
//        root.left = node1;
        root.right = node2;
//        node2.left = node3;
        node2.left = node4;
        System.out.println(new Solution().preorderTraversal(root));
    }

    static class Solution {
        List<Integer> list = new ArrayList<>();
        public List<Integer> preorderTraversal(TreeNode root) {
            dfs(root);
            return list;
        }
        private void dfs(TreeNode node){
            if(null == node) return ;
            list.add(node.val);
            if(null != node.left) dfs(node.left);
            if(null != node.right) dfs(node.right);
        }
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) {
            val = x;
        }
    }
}